package com.hfut.service.impl;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.hfut.dao.GlobalDao;
import com.hfut.model.Book;
import com.hfut.model.RequestBook;
import com.hfut.service.GlobalService;
import com.hfut.util.PageBean;

@Component
public class GlobalServiceImpl implements GlobalService{
	private GlobalDao globalDao;
	
	public PageBean getPageBean(int pageSize, int page) {
		PageBean pageBean = new PageBean();

		String hql = "from Book b";

		int allRows = globalDao.getAllRowCount(hql);

		int totalPage = pageBean.getTotalPages(pageSize, allRows);

		int currentPage = pageBean.getCurPage(page);

		int offset = pageBean.getCurrentPageOffset(pageSize, currentPage);

		List<Book> list = globalDao.queryByPage(hql, offset, pageSize);

		pageBean.setList(list);
		pageBean.setAllRows(allRows);
		pageBean.setCurrentPage(currentPage);
		pageBean.setTotalPage(totalPage);

		return pageBean;
	}

	public GlobalDao getGlobalDao() {
		return globalDao;
	}

	@Resource
	public void setGlobalDao(GlobalDao globalDao) {
		this.globalDao = globalDao;
	}

	@Override
	public List<RequestBook> queryRequest() {
		String hql = "from RequestBook b order by b.id desc";
		return globalDao.queryRequest(hql);
	}

	@Override
	public List<Book> queryBook() {
		String hql = "from Book b order by b.id desc";
		return globalDao.queryBook(hql);
	}

	@Override
	public Book findBookbyId(int id) {
		return globalDao.querryBookById(id);
	}
	
	
}
